package com.example.proyectoformulas;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;


public class FragListadoFormulas extends Fragment {
	public final static String TAG_TABTEMATICA = "tab2";
	public static final String PREFERENCES = "preferences";
	
	private ArrayList<Formula> items;
	Context context = null;
	private View parentView;
	public static ListView listView = null;
	private ItemListaBaseAdapter listAdapter;
	private SharedPreferences preferences = null;
	private SharedPreferences.Editor prefEditor = null;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		main2 mainActivity = (main2) activity;
		//items = mainActivity.getListadoFormulas();
		context = activity;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		context = getActivity();

		setRetainInstance(true);
		setHasOptionsMenu(true);
		
		preferences = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
		prefEditor = preferences.edit();
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		context = getActivity();
		
		parentView = inflater.inflate(R.layout.frag_listado_formulas, container, false);
		
		Collections.sort(items);
		listAdapter = new ItemListaBaseAdapter(context, items);
		
		listView = (ListView) parentView.findViewById(R.id.listView);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//actualizamos el valor "fecha" del objeto que hemos seleccionado y lo guardamos en preferences

				Date now = new Date();
				SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy/HH/mm/ss");			
				
				String fecha = dateFormat.format(now);
				
				PreferencesMat.saveMatDictionary(context, items.get(position).getTematica(), items.get(position).getNombre(), fecha);

		        
				items.get(position).setFecha(now);
				
				//bloquear la lista
				listView.setEnabled(false);	
							
				Fragment fragment = new ContenidoFormula();
				FragmentTransaction fm = getFragmentManager().beginTransaction();
				fm.add(R.id.formulasContent, fragment);
				fm.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
				fm.addToBackStack(null);
				fm.commit();
				
				
			/*	Fragment Faux =getFragmentManager().findFragmentById(R.id.formulasContent);
	        	int a = getFragmentManager().getBackStackEntryCount();
	        	if(Faux instanceof PantallaInicial){
                	Fragment fragment = new TabUltimos();
					FragmentTransaction ft  = getFragmentManager().beginTransaction();
					ft.add(R.id.formulasContent, fragment);
                	ft.addToBackStack(null);
                	ft.commit();
                }	 
	        	else{
	        		FragmentTransaction ft  = getFragmentManager().beginTransaction();
	        		getFragmentManager().popBackStack();
	        		ft.remove(Faux);		        		
	        		ft.commit();
	        		
	        		Fragment fragment = new TabUltimos();
	        		FragmentTransaction ft2  = getFragmentManager().beginTransaction();
	        		ft2.addToBackStack(null);
	        		ft2.add(R.id.formulasContent,fragment);
		       	    ft2.commit();
	        	}*/
			}
		});
		
		listView.setAdapter(listAdapter);
		listAdapter.notifyDataSetChanged();
		
		return parentView;
	
	}
	
	public void setItems(ArrayList<Formula> items){
		this.items = items;
	}

}
